void memcpy(u8int* destination, u8int* source, u32int length) {
    u32int i;
    for(i = 0; i < length; i++) {
        destination[i] = source[i];
    }
}

void memset(u8int* destination, u8int value, u32int length) {
    u32int i;
    for(i = 0; i < length; i++) {
        destination[i] = value;
    }
}

void memcpy16(u16int* destination, u16int* source, u32int length) {
    u32int i;
    for(i = 0; i < length; i++) {
        destination[i] = source[i];
    }
}

void memset16(u16int* destination, u16int value, u32int length) {
    u32int i;
    for(i = 0; i < length; i++) {
        destination[i] = value;
    }
}

int max(int a, int b) {
    if(a > b)
        return a;
    return b;
}

int min(int a, int b) {
    if(a < b)
        return a;
    return b;
}

int strlen(char* str) {
    int i;
    for(i = 0; *(str + i); i++);
    return i;
}

int memcmp(void* ptr1, void* ptr2, int size) {
    int i;
    int diff;
    for(i = 0; i < size; i++) {
        diff = *(u8int*)ptr1 - *(u8int*)ptr2;
        if(diff) {
            return diff;
        }
    }
    return 0;
}

int strcmp(char* str1, char* str2) {

	while( *str1 == *str2 )
	{
		if(*str1 == 0) return 0;
		str1++;
		str2++;
	}
	return *str1 - *str2;
}

char* strcpy(char* destination, char* source) {
    memcpy(destination, source, strlen(source) + 1);
}

char* strcat(char* destination, char* source) {
    char* start = destination + strlen(destination);
    memcpy(start, source, strlen(source) + 1);
}

char* strrev(char* source) {
    char buffer[strlen(source) + 1];
    int i, j;
    for(i = strlen(source) - 1, j = 0; i >= 0; i--, j++)
        buffer[j] = source[i];
    buffer[j] = '\0';
    strcpy(source, buffer);
    return source;
}

char* itoa(u32int val, int base) {
    int i;
    for(i = 0; i < 32; i++)
        itoa_buffer[i] = 0;
    for(i = 30; val && i; i--, val = val / base)
        itoa_buffer[i] = "0123456789ABCDEF"[val % base];
    if(strlen(&itoa_buffer[i+1]) == 0)
        return "0";
    return &itoa_buffer[i+1];               
}

int _atoi(char *str) {
    unsigned int answer=0;
    int base = 10;

    if (*str == '0' && str[1] == 'x') {
        base = 0x10;
        str += 2;
    }

    while (*str) {
        answer *= base;
        if (*str > '9') 
            answer += (*str | 0x20) - 'a' + 0xa;
        else 
            answer += *str - '0';
        str++;
    }
    return answer;
}

u32int from_linear(u32int seg, u32int linear) {
    return linear - (seg * 0x10);
}

u32int to_linear(u32int seg, u32int off) {
	return (seg & 0xFFFF) * 0x10 + off;
}
